package com.yonyou.brigade.startup;

import com.yonyou.iuap.ucf.log.filter.MDCLogFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.yonyou.ybzboot"})
@ComponentScan({"com.yonyou.brigade", "com.yonyou.ybzboot"})
@EnableAsync
public class StartupApplication extends SpringBootServletInitializer {
    private static final Logger logger = LoggerFactory.getLogger(StartupApplication.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(StartupApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(StartupApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
        logger.info(StartupApplication.class.getName() + " 启动成功！！！ ");
    }

    @Bean
    public FilterRegistrationBean registerBrigadeLogFilter() {
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        //增加日志过滤器
        MDCLogFilter logFilter = new MDCLogFilter();
        // 设置过滤器名称, 执行顺序, 拦截url
        String filterName = logFilter.getClass().getSimpleName();
        int order = 30;
        List<String> urlPatterns = new ArrayList<>(1);
        urlPatterns.add("/*");
        filterBean.setFilter(logFilter);
        filterBean.setName(filterName);
        filterBean.setOrder(order);
        filterBean.setUrlPatterns(urlPatterns);
        return filterBean;
    }
}
